﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Baimasoft.Manage.SqlServerDAL;

public partial class Common_OrgTree : System.Web.UI.Page
{
    private string rightType = "1"; //权限类别 1 表示档案工资，0表示活动工资。

    private string parmStr = ""; //权限参数字符串。


    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.IsPostBack == false)
        {

            if (Request.QueryString["RightType"] != null)
            {
                rightType = Request.QueryString["RightType"];

                if (rightType == "1")
                {
                    parmStr = "&RightType=1";
                }
                else
                {
                    parmStr = "&RightType=0";
                }
            }

            #region 加载机构信息
            DataTable dtOrgs = SysOrgDAL.GetSysOrgOtherInfosByOrgIds(AspNetHelper.GetRightOrgIds());
            if (dtOrgs == null) return;

            TreeNode rootNode;
            //获取根节点

            foreach (DataRow dr in dtOrgs.Rows)
            {

                rootNode = new TreeNode();
                rootNode.Value = dr["ID"].ToString();
                rootNode.Text = dr["Name"].ToString();
                rootNode.ImageUrl = "~/PayManage/XmlTree/icon/close.gif";
                //rootNode.SelectAction = TreeNodeSelectAction;
                //rootNode.PopulateOnDemand = true;
                //rootNode.NavigateUrl = function.Url;
                this.tvOrgTree.Nodes.Add(rootNode);
                //rootNode.Expand();

            }
            #endregion

            #region 加载人员信息
            DataTable dtEmps = SysOrgDAL.GetSysPersInfosByDeptId(0);
            if (dtEmps == null) return;
            //获取根节点

            foreach (DataRow dr in dtEmps.Rows)
            {

                rootNode = new TreeNode();
                rootNode.Value = dr["ID"].ToString();
                rootNode.Text = dr["Name"].ToString() + "(" + dr["PersCode"].ToString() + ")";
                rootNode.ImageUrl = "~/PayManage/XmlTree/icon/file2.gif";
                //rootNode.SelectAction = TreeNodeSelectAction;
                //rootNode.PopulateOnDemand = true;
                rootNode.NavigateUrl = "../PayManage/EmpCom_File_Edit.aspx?ID=" + dr["ID"].ToString() + parmStr;
                this.tvOrgTree.Nodes.Add(rootNode);
                //rootNode.Expand();

            }
            #endregion
        }
    }

    protected void tvOrgTree_SelectedNodeChanged(object sender, EventArgs e)
    {
        if (this.tvOrgTree.SelectedNode == null || this.tvOrgTree.SelectedNode.ChildNodes.Count > 0 || this.tvOrgTree.SelectedNode.NavigateUrl != string.Empty) return;

        int orgId = int.Parse(this.tvOrgTree.SelectedNode.Value);
        #region 加载机构信息
        DataTable dt = SysOrgDAL.GetSysOrgOtherInfosByParentId(orgId);

        TreeNode rootNode;
        //获取根节点

        foreach (DataRow dr in dt.Rows)
        {

            rootNode = new TreeNode();
            rootNode.Value = dr["ID"].ToString();
            rootNode.Text = dr["Name"].ToString();
            rootNode.ImageUrl = "~/PayManage/XmlTree/icon/close.gif";
            //rootNode.ImageToolTip = "";
            //rootNode.PopulateOnDemand = true;

            //rootNode.NavigateUrl = function.Url;
            this.tvOrgTree.SelectedNode.ChildNodes.Add(rootNode);

        }

     

        #endregion

        #region 加载人员信息
        //获取权限参数
        if (Request.QueryString["RightType"] != null)
        {
            rightType = Request.QueryString["RightType"];

            if (rightType == "1")
            {
                parmStr = "&RightType=1";
            }
            else
            {
                parmStr = "&RightType=0";
            }
        }

        DataTable dtEmps = SysOrgDAL.GetSysPersInfosByDeptId(orgId);
        if (dtEmps == null) return;
        //获取根节点

        foreach (DataRow dr in dtEmps.Rows)
        {

            rootNode = new TreeNode();
            rootNode.Value = dr["ID"].ToString();
            rootNode.Text = dr["Name"].ToString() + "(" + dr["PersCode"].ToString() + ")";
            rootNode.ImageUrl = "~/PayManage/XmlTree/icon/file2.gif";
            //rootNode.SelectAction = TreeNodeSelectAction;
            //rootNode.PopulateOnDemand = true;
            //EmpCom_Info_Main.aspx?PersID=219
            rootNode.NavigateUrl = "../PayManage/EmpCom_File_Edit.aspx?ID=" + dr["ID"].ToString() + parmStr;
            this.tvOrgTree.SelectedNode.ChildNodes.Add(rootNode);
  

        }
        #endregion

        if (this.tvOrgTree.SelectedNode.ChildNodes.Count > 0) 
                this.tvOrgTree.SelectedNode.Expand();
    }
    protected void tvOrgTree_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
    {
        //if(e.Node.ChildNodes.Count > 0)
           e.Node.ImageUrl = "~/PayManage/XmlTree/icon/open.gif";
    }
    protected void tvOrgTree_TreeNodeCollapsed(object sender, TreeNodeEventArgs e)
    {
        e.Node.ImageUrl = "~/PayManage/XmlTree/icon/close.gif";
    }
    protected void btnRefreshNode_Click(object sender, EventArgs e)
    {
        if (this.tvOrgTree.SelectedNode != null)
        {
            this.tvOrgTree.SelectedNode.ChildNodes[3].Text += "aaaaa";
        }
        //if (this.tvOrgTree.SelectedNode != null)
        //{
        //    this.tvOrgTree.SelectedNode.ChildNodes.Add(new TreeNode("newNode", "newNode", "~/PayManage/XmlTree/icon/file2.gif"));
        //}
    }
}
